package com.huaxiafinance.management.controller;

import com.huaxiafinance.management.dto.request.AddSupplierDto;
import com.huaxiafinance.management.dto.request.QuerySupplierList;
import com.huaxiafinance.management.dto.request.UpdateSupplierDto;
import com.huaxiafinance.management.mapper.MallSupplierMapper;
import com.huaxiafinance.management.service.MallSupplierService;
import com.huaxiafinance.management.util.MessageResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;


@Controller
@RequestMapping("/supplier")
public class MallSupplierController {

  @Autowired
  private MallSupplierService mallSupplierService;
  @Autowired
  private MallSupplierMapper mallSupplierMapper;
  private final static Logger log = Logger.getLogger(MallSupplierController.class);

  /**
   * 供应商列表
   */
  @RequestMapping(value = "/querySupplierList", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String querySupplierList(QuerySupplierList dto) {
    MessageResult messageResult = new MessageResult();
    try {
      messageResult.setData(mallSupplierService.querySupplierList(dto));
      messageResult.setRows(mallSupplierMapper.countSupplierList(dto));
      messageResult.setSuccessResult();
    } catch (Exception e) {
      messageResult.setFailureResult();
      log.info("供应商列表异常" + e);
    }
    log.info("供应商列表返回参数" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }

  /**
   * 添加供应商户
   */
  @RequestMapping(value = "/addSupplier", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String addSupplier(@Valid @RequestBody AddSupplierDto dto, BindingResult bindingResult) {
    MessageResult messageResult = new MessageResult();
    if (bindingResult.hasErrors()) {
      messageResult.setFailureResult(bindingResult);
    } else {
      try {
        messageResult = mallSupplierService.addSupplier(dto);
      } catch (Exception e) {
        messageResult.setFailureResult();
        log.info("添加供应商异常原因" + e);
      }
    }
    log.info("添加供应商异返回参数" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }

  /**
   * 供应商合作关系修改
   */
  @RequestMapping(value = "/updateCooperate", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String updateCooperate(Integer id, Integer states) {
    MessageResult messageResult = new MessageResult();
    try {
      mallSupplierService.updateCooperate(id, states);
      messageResult.setSuccessResult();
    } catch (Exception e) {
      messageResult.setFailureResult();
      log.info("供应商合作关系修改异常" + e);
    }
    log.info("供应商合作关系修改返回参数" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }

  /**
   * 修改供应商户
   */
  @RequestMapping(value = "/updateSupplier", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String updateSupplier(@Valid @RequestBody UpdateSupplierDto dto, BindingResult bindingResult) {
    MessageResult messageResult = new MessageResult();
    if (bindingResult.hasErrors()) {
      messageResult.setFailureResult(bindingResult);
    } else {
      try {
        messageResult = mallSupplierService.updateSupplier(dto);
      } catch (Exception e) {
        messageResult.setFailureResult();
        log.info("修改供应商异常原因" + e);
      }
    }
    log.info("修改供应商返回参数" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }


  /**
   * 供应商详情
   */
  @RequestMapping(value = "/queryOne", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String queryOne(Integer id) {
    MessageResult messageResult = new MessageResult();
    try {
      messageResult.setData(mallSupplierService.queryOne(id));
      messageResult.setSuccessResult();
    } catch (Exception e) {
      messageResult.setFailureResult();
      log.info("供应商详情异常" + e);
    }
    log.info("供应商详情返回参数" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }

  @RequestMapping(value = "/queryNameList", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String queryNameList(Integer state) {
    MessageResult messageResult = new MessageResult();
    try {
      messageResult.setData(mallSupplierService.queryNameList(state));
      messageResult.setSuccessResult();
    } catch (Exception e) {
      messageResult.setFailureResult();
      log.info("供应商下拉列表详情异常" + e);
    }
    log.info("供应商下拉列表详情返回参数" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }
}
